<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
	Integer pid = input.getInt("pid", 0);
	Integer uid = token.getUid(); 
	Integer patientUid = input.getInt("patient_uid", -1);
	if (patientUid != -1) {
		uid = patientUid;
		if (!validateReportAuth(token, patientUid)) {
			msg.setErrcodeAndErrmsg(30021, "您没有权限查看该患者的体检报告");
			Tools.encryptPrint(out, msg); 
			return;
		}
		UserProfile selfProfile = selfProfileByUid(uid);
		if (selfProfile != null) 
			pid = selfProfile.getId();
	}
	Integer branch = input.getInt("branch", 1);
	String where = "";
	if (hospital != 999)
		where = " and hospital=" + hospital  ;
	Map<Integer, UserProfile> userprofileMap = userprofileByUid(uid);
	Map<String, Integer> cardMap = new HashMap<String, Integer>();
	String hql = "";
	if (pid == 0) { // 查全部就诊人
		String cards = "'-1'";
		for (Integer key : userprofileMap.keySet()) {
			UserProfile up = userprofileMap.get(key);
			String cardNumber = up.getCardNumber();
			cardMap.put(cardNumber, key);
			cards += ",'" + cardNumber + "'";
		}
		hql = "select distinct new map(checkNumber as check_number, DATE_FORMAT(checkDate, '%Y-%m-%d') as check_date, name as name, idenditiyNumber as card_number, hospital as hospital) from PhysicalCheck where idenditiyNumber in (" + cards + ") " + where  + " order by checkDate desc";
	} else {
		UserProfile userprofile = userprofileMap.get(pid);
		if (userprofile == null) {
			msg.setErrcodeAndErrmsg(30021, "pid不属于该用户");
			Tools.encryptPrint(out, msg); 
			return;
		}
		String cardNumber = userprofile.getCardNumber();
		hql = "select distinct new map(checkNumber as check_number, DATE_FORMAT(checkDate, '%Y-%m-%d') as check_date, name as name, idenditiyNumber as card_number, hospital as hospital) from PhysicalCheck where idenditiyNumber = '" + cardNumber + "' " + where + "  order by checkDate desc";
	}
	List checkList = QBuilder.db("basedb").queryHql(hql);
	for (int i = 0; i < checkList.size(); i++) {
		Map ma = (Map)checkList.get(i);
		String cardNumber = Tools.getDesc(ma.get("card_number"), "");
		ma.put("pid", cardMap.get(cardNumber));
	}
	completeHospitalInfo(checkList);
	msg.put("data", checkList);
	Tools.encryptPrint(out, msg);
%>